﻿using System;
using System.Configuration;
using Cms.Application.Admin.Presenters;
using Cms.Application.Admin.Views;
using Cms.Web.Framework.Controls;
using Cms.Web.Framework.Extensions;
using iLib.Web.UI;

namespace Cms.Web.UI.Admin
{
    public partial class EditCustomDashboardGadget : AdminPage, IEditCustomDashboardGadgetView
    {
        private EditCustomDashboardGadgetPresenter _presenter;

        #region IEditCustomDashboardGadgetView Members

        public int GadgetID
        {
            get
            {
                if (hidGadgetID.Value == "")
                    hidGadgetID.Value = QueryString.GetInt32ID("id").ToString();
                return int.Parse(hidGadgetID.Value);
            }
        }


        public void SetGadgetID(int id)
        {
            hidGadgetID.Value = id.ToString();
        }


        public string CurrentTab
        {
            get { return tabStrip.CurrentValue; }
        }


        public string Filename
        {
            get { return txtFilename.Text; }
            set { txtFilename.Text = value; }
        }


        public string ContextTranslationFile
        {
            get { return "Admin/Translations/EditCustomDashboardGadget.xml"; }
        }


        public string Xml
        {
            get { return hidXml.Value; }
            set { hidXml.Value = value; }
        }


        public string Code
        {
            get { return hidCode.Value; }
            set { hidCode.Value = value; }
        }

        #endregion

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _presenter = new EditCustomDashboardGadgetPresenter(this);
            if (!Page.IsPostBack)
            {
                if (GadgetID > 0)
                    tabStrip.SelectedValue = Request.QueryString["tab"];
                _presenter.Initialize();
                HeadingIcon = litIcon.Text;
            }
            _presenter.UpdateHeading();
            Page.AddHeaderScriptInclude(ConfigurationManager.AppSettings["jQueryPath"]);
        }


        private string GetClientClickScript(AdminTabItem ti)
        {
            return "cms.Edit.ShowTabOverlays(); " +
                   "updateCode(); " +
                   "if (isDirty()) {" +
                   "return cms.Dialogs.Confirm(" +
                   "function (status) { " +
                   "document.getElementById('ctl00_body_hidSaveXml').value = status;" +
                   "document.getElementById('ctl00_body_hidSaveCode').value = status;" +
                   "document.getElementById('ctl00_tabs_hidSave').value = status;" +
                   "var gadgetID = document.getElementById('ctl00_tabs_hidGadgetID').value;" +
                   "if ((gadgetID == '' || gadgetID == '-1') && status != 'ok') " +
                   "cms.Edit.HideTabOverlays(); " +
                   "else " +
                   "__doPostBack('" + ti.UniqueID + "$" + ti.ID + "','');" +
                   "}, " +
                   "'" + GetText(this, "SaveInfoHeader") + "', " +
                   "'" + GetText(this, GetSaveInfoMessageKey()) + "', " +
                   "'" + GetText(this, "SaveInfoOK") + "', " +
                   "'" + GetText(this, "SaveInfoCancel") + "', " +
                   "true, " +
                   "null" +
                   ");" +
                   "}";
        }


        private string GetSaveInfoMessageKey()
        {
            return GadgetID > 0 ? "OptionalSaveInfoMessage" : "ForceSaveInfoMessage";
        }


        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            tiProperties.OnClientClick = GetClientClickScript(tiProperties);
            tiXml.OnClientClick = GetClientClickScript(tiXml);
            tiCode.OnClientClick = GetClientClickScript(tiCode);
            if (tabStrip.CurrentValue != tabStrip.SelectedValue && hidSave.Value != "ok")
                _presenter.RefreshProperties();
        }


        protected override void OnPreRenderComplete(EventArgs e)
        {
            base.OnPreRenderComplete(e);
            if (tabStrip.SelectedValue == "properties")
            {
                upnlProperties.Update();
            }
            ClientScriptProxy.Current.RegisterStartupScript(tabStrip, GetType(),
                                                            "updatePanels",
                                                            "updatePanels('" + tabStrip.SelectedValue + "');", true);
            upnlButtons.Update();
        }


        protected void tabStrip_Click(string value)
        {
            if ((CurrentTab == "properties" && hidSave.Value == "ok") ||
                (CurrentTab == "xml" && hidSaveXml.Value == "ok") ||
                (CurrentTab == "code" && hidSaveCode.Value == "ok"))
                _presenter.Save();
            if (_presenter.IsValid)
            {
                ClientScriptProxy.Current.RegisterStartupScript(tabStrip, GetType(), "init", "init();", true);
            }
            upnlXml.Update();
            upnlCode.Update();
            upnlProperties.Update();
        }


        protected void lbSave_Click(object sender, EventArgs e)
        {
            _presenter.Save();
            if (_presenter.IsValid)
                Response.Redirect("DashboardGadgets.aspx");
        }
    }
}